package info.openlvb.openface.client.base.person;

import info.openlvb.openface.client.handler.AddPersonListener;
import info.openlvb.openface.client.handler.ChkMail_Handler;
import info.openlvb.openface.client.handler.ChkRepPersoHandler;
import info.openlvb.openface.client.handler.ClearPersonListener;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;
import com.gwtext.client.widgets.form.event.FieldListenerAdapter;

public class AddPersonPanel extends VerticalPanel{

	TextField repPerso,login,prenom,nom,description,mail,password,confirmpassword;
	CheckBox chkmail,chkrepperso;
	ComboBox cb;
	
	public AddPersonPanel(){
		FormPanel formPanel = new FormPanel();
		formPanel.setFrame(true);
        formPanel.setTitle("Ajout d'un utilisateur");
        formPanel.setLabelWidth(150);
		
        login = new TextField("Login", "login", 250);
        login.setAllowBlank(false);
        login.setDisabled(true);
        formPanel.add(login);
		
        nom = new TextField("Nom", "nom", 150);
        nom.setAllowBlank(false);
        nom.addKeyPressListener(new EventCallback(){
			public void execute(EventObject e) {
				login.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
			}
        });
        nom.addListener(new FieldListenerAdapter(){
        	public void onBlur(Field field){
        		login.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
        	}
        });
        
        formPanel.add(nom);
		
        prenom = new TextField("Prénom", "prenom", 150);
        prenom.setAllowBlank(false);
        //TODO : attention ici le domaine @ est a aller chercher dans le fichier de conf !!
        prenom.addKeyPressListener(new EventCallback(){
			public void execute(EventObject e) {
				login.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
			}
        });
        prenom.addListener(new FieldListenerAdapter(){
        	public void onBlur(Field field){
        		login.setValue(prenom.getValueAsString()+"."+nom.getValueAsString());
        	}
        });
        formPanel.add(prenom);
        
        //le champs description va nous servir pour qualifier le profil OpenFace
        final Store store = new SimpleStore("profil",new String[]{"utilisateur","administrateur"});
        cb = new ComboBox();
		cb.setForceSelection(true);
		cb.setMinChars(1);
		cb.setFieldLabel("Profil OpenFace");
		cb.setStore(store);
		cb.setDisplayField("profil");
		cb.setMode(ComboBox.LOCAL);
		cb.setTriggerAction(ComboBox.ALL);
		cb.setEmptyText("Profil OpenFace ...");
		cb.setLoadingText("Searching...");
		cb.setTypeAhead(true);
		cb.setSelectOnFocus(true);
		cb.setWidth(200);
        cb.setValueField("utilisateur");
        cb.setAllowBlank(false);
        cb.setEditable(false);
        //description = new TextField("Description","description");
        //formPanel.add(description);
        formPanel.add(cb);
        
        password = new TextField("Password","pass",150);
        password.setPassword(true);
        password.setAllowBlank(false);
        formPanel.add(password);
        confirmpassword = new TextField("Confirm Password","confpass",150);
        confirmpassword.setPassword(true);
        confirmpassword.setAllowBlank(false);
        formPanel.add(confirmpassword);
        
        chkmail = new CheckBox("Activer une boite mail ?");
        chkmail.addClickHandler(new ChkMail_Handler(this));
        formPanel.add(chkmail);
        
        mail = new TextField("Mail", "mail", 250);
        mail.setAllowBlank(false);
        mail.setVtype(VType.EMAIL); 
        mail.setDisabled(true);
        formPanel.add(mail);
        
        chkrepperso = new CheckBox("Activer un répertoire perso ?");
        chkrepperso.addClickHandler(new ChkRepPersoHandler(this));
        
        formPanel.add(chkrepperso);
        repPerso = new TextField("Répertoire personnel","persopath",250);
        repPerso.setDisabled(true);
        formPanel.add(repPerso);
        
        
        Button addperson = new Button("Ajouter l'utilisateur");
        addperson.addListener(new AddPersonListener(this));
        formPanel.addButton(addperson);
        
        Button clearperson = new Button("Annuler");
        clearperson.addListener(new ClearPersonListener(this));
        formPanel.addButton(clearperson);
        
        add(formPanel);
	}
	public TextField getLogin() {
		return login;
	}
	public void setLogin(TextField login) {
		this.login = login;
	}
	public TextField getMail() {
		return mail;
	}
	public void setMail(TextField mail) {
		this.mail = mail;
	}
	public TextField getRepPerso() {
		return repPerso;
	}
	public void setRepPerso(TextField repPerso) {
		this.repPerso = repPerso;
	}
	public TextField getPrenom() {
		return prenom;
	}
	public void setPrenom(TextField prenom) {
		this.prenom = prenom;
	}
	public TextField getNom() {
		return nom;
	}
	public void setNom(TextField nom) {
		this.nom = nom;
	}
//	public TextField getDescription() {
//		return description;
//	}
	public String getDescription(){
		return cb.getValue();
	}
	public void setDescription(TextField description) {
		this.description = description;
	}
	public TextField getPassword() {
		return password;
	}
	public void setPassword(TextField password) {
		this.password = password;
	}
	public TextField getConfirmpassword() {
		return confirmpassword;
	}
	public void setConfirmpassword(TextField confirmpassword) {
		this.confirmpassword = confirmpassword;
	}
	public CheckBox getChkmail() {
		return chkmail;
	}
	public void setChkmail(CheckBox chkmail) {
		this.chkmail = chkmail;
	}
	public CheckBox getChkrepperso() {
		return chkrepperso;
	}
	public void setChkrepperso(CheckBox chkrepperso) {
		this.chkrepperso = chkrepperso;
	}
}
